<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script src="/javascript/jquery.alerts.js" type="text/javascript"></script>
<script type="text/javascript" src="/javascript/jquery.tooltip.execute.js"></script>
<script type="text/javascript" src="/javascript/jquery.tooltip.v.1.1.js"></script>
<link href="/stylesheets/jquery.alerts.css" rel="stylesheet" type="text/css" media="screen" />

<script type="text/javascript">
var optionsNum=5;
var i=1;
var valList=[0,0,0,0,0];
// Init
function updateList() {
	document.forms['stockbuyform'].numofstocks.value=valList;
}
function deleteRow(selected) {
	var stockBar = document.getElementById("buydiv");
	var flag=0, count=0;
	stockBar.removeChild(document.getElementById("formDiv[" + selected + "]"));
	valList[selected]=0;
	document.getElementById("addlink").style.visibility='visible';
	for (i=0;i<=optionsNum-1;i++) {
		if (valList[i]==1) {
			// Found first available space
			if (flag==0) {
				selected=i;
				flag=1;
			}
			count++;
		}
	}
	// Disable link if all options are selected
	if (count==1) document.getElementById("link" + selected).style.cssText="font-style:italic;visibility:hidden;";
	updateList();
}

		
function insertAdvice(keywordChosen) {
	for (i=optionsNum-1;i>=0;i--) {
		if (valList[i]==1) {
			selectedadvice=i;
			break;
		}
	}
	$('[name=setext[' + selectedadvice + ']]').val(keywordChosen);
}
function generateRow(phase) {
	// Var for number of options
	var flag=0, count=0;
	var selected;
	var stockBar = document.getElementById("buydiv");	
	
	var newDiv = document.createElement("div");
    var elementName = document.createElement("input");
	var elementVal = document.createElement("input");
	var a = document.createElement("a");

	for (i=0;i<=optionsNum-1;i++) {
		if (valList[i]==0) {
			// Found first available space
			if (flag==0) {
				selected=i;
				flag=1;
			}
			count++;
		}
		else {
			document.getElementById("link" + i).style.cssText="font-style:italic;visibility:visible;";
		}
	}
	// Disable link if all options are selected
	if (count==1) document.getElementById("addlink").style.cssText="visibility:hidden;";
	// Error occured (should never reach this point because link is disabled)
	else if (count==0) return "Error";
	
	newDiv.setAttribute("id", "formDiv[" + selected + "]");
	// Add remove button only if this is not the first row
	a.setAttribute("href", "javascript:deleteRow(" + selected + ")");
	a.setAttribute("name", "link[" + selected + "]");
	a.setAttribute("id", "link" + selected);
	// If row is first on page, hide the remove button
	if (phase!=0)
		a.style.cssText="font-style:italic;visibility:visible;";
	else
		a.style.cssText="visibility:hidden;";	
	a.appendChild(document.createTextNode("(remove)"));
	
	elementName.setAttribute("type", "text");
	elementName.setAttribute("name", "setext[" + selected + "]");
	elementName.setAttribute("id", "setext" + selected);
	if (phase!=0)
		elementName.setAttribute("value", "Stock Name (In English)");
	else
		elementName.setAttribute("value", "{{ stockname }}");
	elementName.setAttribute("MAXLENGTH", "{{ maxStockLength }}");
	elementName.onclick = function() { 
		if(this.value=='Stock Name (In English)'){
			this.value='';
		} 
	};
	
	elementVal.setAttribute("type", "text");
	elementVal.setAttribute("name", "sevalue[" + selected + "]");
	elementVal.setAttribute("id", "sevalue" + selected);
	elementVal.setAttribute("value", "Amount To Invest");
	elementVal.setAttribute("MAXLENGTH", "{{ MaxAmountValue }}");
	elementVal.onclick = function() { 
		if(this.value=='Amount To Invest'){
			this.value='';
		}  
	};
	
	newDiv.appendChild(elementName);
	newDiv.appendChild(document.createTextNode(String.fromCharCode(160)+String.fromCharCode(160)+String.fromCharCode(160)))
	newDiv.appendChild(elementVal);
	newDiv.appendChild(a);
	stockBar.appendChild(newDiv);
	valList[selected]=1;
	document.forms['stockbuyform'].numofstocks.value=valList;
	updateList();
}


		 // JQuery Code
		$(document).ready(function() {

			$("a.toolclass").simpletooltip();
			$("a.toolclass2").simpletooltip();
			
			generateRow(0);
			$('#ETFuyform').submit(function() {
				res=ValidateETF()
				if (res==0) {
					jAlert("Select an ETF category", "Attention");
					return false;
				}
				else if (res==-1) {
					jAlert("Please insert valid investment value", "Attention");
					return false;
				}
					
				else return true;
			});
			$('#stockbuyform').submit(function() {
				res=ValidateRows()
				if (res==0) {
					jAlert("Please use English letters and numbers only", "Attention");
					return false;
				}
				else if (res==-1) {
					jAlert("Stock name must not exceed 50 characters", "Attention");
					return false;
				}
				else if (res==-2) {
					jAlert("Please insert valid investment value", "Attention");
					return false;
				}
					
				else return true;
			});
			$('#recommend-div select').change(function() {
				var chosenSelected=$(this).find("option:selected").val();
				if (chosenSelected!=0) {
					$.getJSON('/ajax/buyadvice', {'categoryname':chosenSelected}, function(data) {
							if (data.status == "success") {
								var row1="";
								var row2="";
								for (j=0;j<6;j++) {
									if (j<=2) {
										row1+="<td>";
										if (data.retAdvice[j]!="")
											row1+="<a href=\"javascript:insertAdvice('" + data.retAdvice[j] + "')\">" + data.retAdvice[j] + "</a>";
										row1+="</td>"
									}
									else {
										row2+="<td>";
										if (data.retAdvice[j]!="")
											row2+="<a href=\"javascript:insertAdvice('" + data.retAdvice[j] + "')\">" + data.retAdvice[j] + "</a>";
										row2+="</td>";
									}
								}
								$('#adviceres').html("<table width='350'><tr>" + row1 + "</tr>"
									+ "<tr>" + row2 + "</tr></table>");
							}
							});
						}
				      }
					);
			return false;
		});
		
		function ShowPost(){
			$("div.add-comment").hide();
			$("div.comment-form").show();
		}


		function ValidateRows() {
			for (i=0;i<=optionsNum-1;i++) {
				if (valList[i]==1) {
					if ($('#setext' +i).val().search(/[^a-zA-Z0-9 ]/g) != -1 || $('#setext' +i).val()=='') return 0;
					else if ($('#setext' +i).val().length>{{ maxStockLength }}) return -1;
					else if ($('#sevalue' +i).val()=='' || $('#sevalue' +i).val()!=parseFloat($('#sevalue' +i).val()) || parseFloat($('#sevalue' +i).val())=='0') return -2;
				}
			}
			
			return 1;
			
		}

		function ValidateETF() {
			
			if (!$('#cattext').val()) return 0;
			else if ($('#ETFvalueid').val()=='' || $('#ETFvalueid').val()!=parseFloat($('#ETFvalueid').val()) || parseFloat($('#ETFvalueid').val())=='0') return -1;
			return 1;
			
		}



</script> 